package com.lzx.adx.report.util.helper;

import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.toutiao.request.TouTiaoRequest;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class TouTiaoHelper {


    public static String covertUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        JSONObject jsonObject = JSONObject.parseObject(accountDTO.getDspExtend());
        String channel = jsonObject.getString("channel");
        String advertisingSpaceId=jsonObject.getString("advertisingSpaceId");
        String taskId=jsonObject.getString("taskId");
        String adAgent=jsonObject.getString("adAgent");
        TouTiaoRequest touTiaoRequest= JSON.parseObject( reportDTO.getExtend(),TouTiaoRequest.class);

        String callBackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());

        Map<String,Object> prams=new HashMap<>();
        prams.put("channel",channel);
        prams.put("advertisingSpaceId",advertisingSpaceId);
        prams.put("taskId",taskId);
        prams.put("adAgent",adAgent);
        prams.put("app","45");
        prams.put("adid",reportDTO.getCampaignId());
        prams.put("cid",reportDTO.getContentId());
        prams.put("adname",touTiaoRequest.getAidName());
        prams.put("adGroupId",reportDTO.getAdgroupId());
        prams.put("imeiMd5",reportDTO.getImei());
        prams.put("oaid",touTiaoRequest.getOaid());
        prams.put("oaidMd5",touTiaoRequest.getOaidMd5());
        prams.put("callbackUrl", URLEncoderDecoderTool.encodeURL(callBackUrl));

        String paramsUrl= URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl()+"&"+paramsUrl;
    }


}
